﻿using Mapster;
using PLC.Application.Stations.Services;
using PLC.Application.StationVariables.Services;
using PLC.Interface.Stations;
using PLC.Interface.StationVariables;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using YWM;
using YWM.Dto;

namespace PLC.Application.Stations
{
    public class StationAppService : IStationAppService
    {
        private readonly IStationService _stationService = YApp.Resolve<IStationService>();

        private readonly IStationVariableService _stationVariableService = YApp.Resolve<IStationVariableService>();

        public async Task<IReadOnlyList<StationResultDto>> GetTopList(int top = 6)
        {
            PagedResultDto<StationResultDto> stationPagedResultDto = await _stationService.GetAll(new StationResultRequestDto { MaxResultCount = top, IsActive = true });
            PagedResultDto<StationVariableDto> svPagedResultDto = await _stationVariableService.GetAll(new PagedAndSortedResultRequestDto { MaxResultCount = int.MaxValue });
            foreach (StationResultDto stationResultDto in stationPagedResultDto.Items)
            {
                stationResultDto.StationVariable = (svPagedResultDto.Items.FirstOrDefault(sv => sv.StationId == stationResultDto.Id) ?? new StationVariableDto()).Adapt<StationVariableResultDto>();
            }
            return stationPagedResultDto.Items;
        }
    }
}
